package cn.com.callback

import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import org.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.com.elite.EliteBean
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse;
import org.json.JSONObject;

class autoGetPost implements ServiceBackInvoke {

    private static final Logger log = LoggerFactory.getLogger(autoGetPost.class);

    @Override
    String doCallBack(EliteBean eliteBean, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse) {

        def httpURL = KHDBConfig.getAPIURL(eliteBean, "getPostByMarketId");
        def result = Util.doHttpPost(httpURL, "", "UTF-8", 5000, 0);
        JSONObject JsonDepartment = new JSONObject(result);
        log.info("[获取岗位接口调用结果]:" + JsonDepartment);
        int re_status = JsonDepartment.getInt("status")
        if (re_status == 200) {
            JSONArray jsonArray = JsonDepartment.getJSONArray("data");
            int dataArrayLen = jsonArray.length();
            JSONObject dataObject = null;
            for (int i = 0; i < dataArrayLen; i++) {
                dataObject = jsonArray.getJSONObject(i);
                def id = KHUtil.GetJV(dataObject, "id");//岗位id
                def postName = KHUtil.GetJV(dataObject, "postName");//岗位名称
                def status = KHUtil.GetJV(dataObject, "status");//使用状态

                if(status==0){
                    status=1;
                }else if(status==1){
                    status=0;
                }

                def selectSql="select count(*) COUNT from dw_post where id='" + id + "'";
                def sqlResult= eliteBean.chaxun_sql(selectSql, 'dataSource');
                def row1 =sqlResult.get(0).get("COUNT");
                if(row1<=0){
                    def insertSql = "insert into dw_post(id,postname,status,createddate) values('" + id + "','" + postName + "','" + status + "',now())";
                    def rwo2= eliteBean.update(insertSql, 'dataSource');
                    log.debug("执行dw_post数据新增SQL结果:" + rwo2);
                }else{
                    def updateSql = "update dw_post set postname='" + postName + "',modifydate=now(),status='"+status+"' where id='"+id+"'";
                    def rwo3= eliteBean.update(updateSql, 'dataSource');
                    log.debug("执行dw_post数据更新SQL结果:" + rwo2);
                }
            }
            return null
        }
    }
}